{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 12.5 \u9632\u6b62\u6b7b\u9501\u7684\u52a0\u9501\u673a\u5236\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6b63\u5728\u5199\u4e00\u4e2a\u591a\u7ebf\u7a0b\u7a0b\u5e8f\uff0c\u5176\u4e2d\u7ebf\u7a0b\u9700\u8981\u4e00\u6b21\u83b7\u53d6\u591a\u4e2a\u9501\uff0c\u6b64\u65f6\u5982\u4f55\u907f\u514d\u6b7b\u9501\u95ee\u9898\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\uff0c\u6b7b\u9501\u95ee\u9898\u5f88\u5927\u4e00\u90e8\u5206\u662f\u7531\u4e8e\u7ebf\u7a0b\u540c\u65f6\u83b7\u53d6\u591a\u4e2a\u9501\u9020\u6210\u7684\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a\u4e00\u4e2a\u7ebf\u7a0b\u83b7\u53d6\u4e86\u7b2c\u4e00\u4e2a\u9501\uff0c\u7136\u540e\u5728\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u9501\u7684\n\u65f6\u5019\u53d1\u751f\u963b\u585e\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7ebf\u7a0b\u5c31\u53ef\u80fd\u963b\u585e\u5176\u4ed6\u7ebf\u7a0b\u7684\u6267\u884c\uff0c\u4ece\u800c\u5bfc\u81f4\u6574\u4e2a\u7a0b\u5e8f\u5047\u6b7b\u3002\n\u89e3\u51b3\u6b7b\u9501\u95ee\u9898\u7684\u4e00\u79cd\u65b9\u6848\u662f\u4e3a\u7a0b\u5e8f\u4e2d\u7684\u6bcf\u4e00\u4e2a\u9501\u5206\u914d\u4e00\u4e2a\u552f\u4e00\u7684id\uff0c\u7136\u540e\u53ea\u5141\u8bb8\u6309\u7167\u5347\u5e8f\u89c4\u5219\u6765\u4f7f\u7528\u591a\u4e2a\u9501\uff0c\u8fd9\u4e2a\u89c4\u5219\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\n\u662f\u975e\u5e38\u5bb9\u6613\u5b9e\u73b0\u7684\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import threading\nfrom contextlib import contextmanager\n\n# Thread-local state to stored information on locks already acquired\n_local = threading.local()\n\n@contextmanager\ndef acquire(*locks):\n    # Sort locks by object identifier\n    locks = sorted(locks, key=lambda x: id(x))\n\n    # Make sure lock order of previously acquired locks is not violated\n    acquired = getattr(_local,'acquired',[])\n    if acquired and max(id(lock) for lock in acquired) >= id(locks[0]):\n        raise RuntimeError('Lock Order Violation')\n\n    # Acquire all of the locks\n    acquired.extend(locks)\n    _local.acquired = acquired\n\n    try:\n        for lock in locks:\n            lock.acquire()\n        yield\n    finally:\n        # Release locks in reverse order of acquisition\n        for lock in reversed(locks):\n            lock.release()\n        del acquired[-len(locks):]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u4f55\u4f7f\u7528\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u5462\uff1f\u4f60\u53ef\u4ee5\u6309\u7167\u6b63\u5e38\u9014\u5f84\u521b\u5efa\u4e00\u4e2a\u9501\u5bf9\u8c61\uff0c\u4f46\u4e0d\u8bba\u662f\u5355\u4e2a\u9501\u8fd8\u662f\u591a\u4e2a\u9501\u4e2d\u90fd\u4f7f\u7528 acquire() \u51fd\u6570\u6765\u7533\u8bf7\u9501\uff0c\n\u793a\u4f8b\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import threading\nx_lock = threading.Lock()\ny_lock = threading.Lock()\n\ndef thread_1():\n    while True:\n        with acquire(x_lock, y_lock):\n            print('Thread-1')\n\ndef thread_2():\n    while True:\n        with acquire(y_lock, x_lock):\n            print('Thread-2')\n\nt1 = threading.Thread(target=thread_1)\nt1.daemon = True\nt1.start()\n\nt2 = threading.Thread(target=thread_2)\nt2.daemon = True\nt2.start()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u6267\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u4f60\u4f1a\u53d1\u73b0\u5b83\u5373\u4f7f\u5728\u4e0d\u540c\u7684\u51fd\u6570\u4e2d\u4ee5\u4e0d\u540c\u7684\u987a\u5e8f\u83b7\u53d6\u9501\u4e5f\u6ca1\u6709\u53d1\u751f\u6b7b\u9501\u3002\n\u5176\u5173\u952e\u5728\u4e8e\uff0c\u5728\u7b2c\u4e00\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5bf9\u8fd9\u4e9b\u9501\u8fdb\u884c\u4e86\u6392\u5e8f\u3002\u901a\u8fc7\u6392\u5e8f\uff0c\u4f7f\u5f97\u4e0d\u7ba1\u7528\u6237\u4ee5\u4ec0\u4e48\u6837\u7684\u987a\u5e8f\u6765\u8bf7\u6c42\u9501\uff0c\u8fd9\u4e9b\u9501\u90fd\u4f1a\u6309\u7167\u56fa\u5b9a\u7684\u987a\u5e8f\u88ab\u83b7\u53d6\u3002\n\u5982\u679c\u6709\u591a\u4e2a acquire() \u64cd\u4f5c\u88ab\u5d4c\u5957\u8c03\u7528\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ebf\u7a0b\u672c\u5730\u5b58\u50a8\uff08TLS\uff09\u6765\u68c0\u6d4b\u6f5c\u5728\u7684\u6b7b\u9501\u95ee\u9898\u3002\n\u5047\u8bbe\u4f60\u7684\u4ee3\u7801\u662f\u8fd9\u6837\u5199\u7684\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import threading\nx_lock = threading.Lock()\ny_lock = threading.Lock()\n\ndef thread_1():\n\n    while True:\n        with acquire(x_lock):\n            with acquire(y_lock):\n                print('Thread-1')\n\ndef thread_2():\n    while True:\n        with acquire(y_lock):\n            with acquire(x_lock):\n                print('Thread-2')\n\nt1 = threading.Thread(target=thread_1)\nt1.daemon = True\nt1.start()\n\nt2 = threading.Thread(target=thread_2)\nt2.daemon = True\nt2.start()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u8fd0\u884c\u8fd9\u4e2a\u7248\u672c\u7684\u4ee3\u7801\uff0c\u5fc5\u5b9a\u4f1a\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53d1\u751f\u5d29\u6e83\uff0c\u5f02\u5e38\u4fe1\u606f\u53ef\u80fd\u50cf\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53d1\u751f\u5d29\u6e83\u7684\u539f\u56e0\u5728\u4e8e\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u8bb0\u5f55\u7740\u81ea\u5df1\u5df2\u7ecf\u83b7\u53d6\u5230\u7684\u9501\u3002 acquire() \u51fd\u6570\u4f1a\u68c0\u67e5\u4e4b\u524d\u5df2\u7ecf\u83b7\u53d6\u7684\u9501\u5217\u8868\uff0c\n\u7531\u4e8e\u9501\u662f\u6309\u7167\u5347\u5e8f\u6392\u5217\u83b7\u53d6\u7684\uff0c\u6240\u4ee5\u51fd\u6570\u4f1a\u8ba4\u4e3a\u4e4b\u524d\u5df2\u83b7\u53d6\u7684\u9501\u7684id\u5fc5\u5b9a\u5c0f\u4e8e\u65b0\u7533\u8bf7\u5230\u7684\u9501\uff0c\u8fd9\u65f6\u5c31\u4f1a\u89e6\u53d1\u5f02\u5e38\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6b7b\u9501\u662f\u6bcf\u4e00\u4e2a\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u90fd\u4f1a\u9762\u4e34\u7684\u4e00\u4e2a\u95ee\u9898\uff08\u5c31\u50cf\u5b83\u662f\u6bcf\u4e00\u672c\u64cd\u4f5c\u7cfb\u7edf\u8bfe\u672c\u7684\u5171\u540c\u8bdd\u9898\u4e00\u6837\uff09\u3002\u6839\u636e\u7ecf\u9a8c\u6765\u8bb2\uff0c\u5c3d\u53ef\u80fd\u4fdd\u8bc1\u6bcf\u4e00\u4e2a\n\u7ebf\u7a0b\u53ea\u80fd\u540c\u65f6\u4fdd\u6301\u4e00\u4e2a\u9501\uff0c\u8fd9\u6837\u7a0b\u5e8f\u5c31\u4e0d\u4f1a\u88ab\u6b7b\u9501\u95ee\u9898\u6240\u56f0\u6270\u3002\u4e00\u65e6\u6709\u7ebf\u7a0b\u540c\u65f6\u7533\u8bf7\u591a\u4e2a\u9501\uff0c\u4e00\u5207\u5c31\u4e0d\u53ef\u9884\u6599\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6b7b\u9501\u7684\u68c0\u6d4b\u4e0e\u6062\u590d\u662f\u4e00\u4e2a\u51e0\u4e4e\u6ca1\u6709\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u7684\u6269\u5c55\u8bdd\u9898\u3002\u4e00\u4e2a\u6bd4\u8f83\u5e38\u7528\u7684\u6b7b\u9501\u68c0\u6d4b\u4e0e\u6062\u590d\u7684\u65b9\u6848\u662f\u5f15\u5165\u770b\u95e8\u72d7\u8ba1\u6570\u5668\u3002\u5f53\u7ebf\u7a0b\u6b63\u5e38\n\u8fd0\u884c\u7684\u65f6\u5019\u4f1a\u6bcf\u9694\u4e00\u6bb5\u65f6\u95f4\u91cd\u7f6e\u8ba1\u6570\u5668\uff0c\u5728\u6ca1\u6709\u53d1\u751f\u6b7b\u9501\u7684\u60c5\u51b5\u4e0b\uff0c\u4e00\u5207\u90fd\u6b63\u5e38\u8fdb\u884c\u3002\u4e00\u65e6\u53d1\u751f\u6b7b\u9501\uff0c\u7531\u4e8e\u65e0\u6cd5\u91cd\u7f6e\u8ba1\u6570\u5668\u5bfc\u81f4\u5b9a\u65f6\u5668\n\u8d85\u65f6\uff0c\u8fd9\u65f6\u7a0b\u5e8f\u4f1a\u901a\u8fc7\u91cd\u542f\u81ea\u8eab\u6062\u590d\u5230\u6b63\u5e38\u72b6\u6001\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u907f\u514d\u6b7b\u9501\u662f\u53e6\u5916\u4e00\u79cd\u89e3\u51b3\u6b7b\u9501\u95ee\u9898\u7684\u65b9\u5f0f\uff0c\u5728\u8fdb\u7a0b\u83b7\u53d6\u9501\u7684\u65f6\u5019\u4f1a\u4e25\u683c\u6309\u7167\u5bf9\u8c61id\u5347\u5e8f\u6392\u5217\u83b7\u53d6\uff0c\u7ecf\u8fc7\u6570\u5b66\u8bc1\u660e\uff0c\u8fd9\u6837\u4fdd\u8bc1\u7a0b\u5e8f\u4e0d\u4f1a\u8fdb\u5165\n\u6b7b\u9501\u72b6\u6001\u3002\u8bc1\u660e\u5c31\u7559\u7ed9\u8bfb\u8005\u4f5c\u4e3a\u7ec3\u4e60\u4e86\u3002\u907f\u514d\u6b7b\u9501\u7684\u4e3b\u8981\u601d\u60f3\u662f\uff0c\u5355\u7eaf\u5730\u6309\u7167\u5bf9\u8c61id\u9012\u589e\u7684\u987a\u5e8f\u52a0\u9501\u4e0d\u4f1a\u4ea7\u751f\u5faa\u73af\u4f9d\u8d56\uff0c\u800c\u5faa\u73af\u4f9d\u8d56\u662f\n\u6b7b\u9501\u7684\u4e00\u4e2a\u5fc5\u8981\u6761\u4ef6\uff0c\u4ece\u800c\u907f\u514d\u7a0b\u5e8f\u8fdb\u5165\u6b7b\u9501\u72b6\u6001\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u4ee5\u4e00\u4e2a\u5173\u4e8e\u7ebf\u7a0b\u6b7b\u9501\u7684\u7ecf\u5178\u95ee\u9898\uff1a\u201c\u54f2\u5b66\u5bb6\u5c31\u9910\u95ee\u9898\u201d\uff0c\u4f5c\u4e3a\u672c\u8282\u6700\u540e\u4e00\u4e2a\u4f8b\u5b50\u3002\u9898\u76ee\u662f\u8fd9\u6837\u7684\uff1a\u4e94\u4f4d\u54f2\u5b66\u5bb6\u56f4\u5750\u5728\u4e00\u5f20\u684c\u5b50\u524d\uff0c\u6bcf\u4e2a\u4eba\n\u9762\u524d\u6709\u4e00\u7897\u996d\u548c\u4e00\u53ea\u7b77\u5b50\u3002\u5728\u8fd9\u91cc\u6bcf\u4e2a\u54f2\u5b66\u5bb6\u53ef\u4ee5\u770b\u505a\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u7ebf\u7a0b\uff0c\u800c\u6bcf\u53ea\u7b77\u5b50\u53ef\u4ee5\u770b\u505a\u662f\u4e00\u4e2a\u9501\u3002\u6bcf\u4e2a\u54f2\u5b66\u5bb6\u53ef\u4ee5\u5904\u5728\u9759\u5750\u3001\n\u601d\u8003\u3001\u5403\u996d\u4e09\u79cd\u72b6\u6001\u4e2d\u7684\u4e00\u4e2a\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6bcf\u4e2a\u54f2\u5b66\u5bb6\u5403\u996d\u662f\u9700\u8981\u4e24\u53ea\u7b77\u5b50\u7684\uff0c\u8fd9\u6837\u95ee\u9898\u5c31\u6765\u4e86\uff1a\u5982\u679c\u6bcf\u4e2a\u54f2\u5b66\u5bb6\u90fd\u62ff\u8d77\u81ea\u5df1\u5de6\u8fb9\u7684\u7b77\u5b50\uff0c\n\u90a3\u4e48\u4ed6\u4eec\u4e94\u4e2a\u90fd\u53ea\u80fd\u62ff\u7740\u4e00\u53ea\u7b77\u5b50\u5750\u5728\u90a3\u513f\uff0c\u76f4\u5230\u997f\u6b7b\u3002\u6b64\u65f6\u4ed6\u4eec\u5c31\u8fdb\u5165\u4e86\u6b7b\u9501\u72b6\u6001\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f7f\u7528\u6b7b\u9501\u907f\u514d\u673a\u5236\u89e3\u51b3\u201c\u54f2\u5b66\u5bb6\u5c31\u9910\u95ee\u9898\u201d\u7684\u5b9e\u73b0\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import threading\n\n# The philosopher thread\ndef philosopher(left, right):\n    while True:\n        with acquire(left,right):\n             print(threading.currentThread(), 'eating')\n\n# The chopsticks (represented by locks)\nNSTICKS = 5\nchopsticks = [threading.Lock() for n in range(NSTICKS)]\n\n# Create all of the philosophers\nfor n in range(NSTICKS):\n    t = threading.Thread(target=philosopher,\n                         args=(chopsticks[n],chopsticks[(n+1) % NSTICKS]))\n    t.start()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u8981\u7279\u522b\u6ce8\u610f\u5230\uff0c\u4e3a\u4e86\u907f\u514d\u6b7b\u9501\uff0c\u6240\u6709\u7684\u52a0\u9501\u64cd\u4f5c\u5fc5\u987b\u4f7f\u7528 acquire() \u51fd\u6570\u3002\u5982\u679c\u4ee3\u7801\u4e2d\u7684\u67d0\u90e8\u5206\u7ed5\u8fc7acquire\n\u51fd\u6570\u76f4\u63a5\u7533\u8bf7\u9501\uff0c\u90a3\u4e48\u6574\u4e2a\u6b7b\u9501\u907f\u514d\u673a\u5236\u5c31\u4e0d\u8d77\u4f5c\u7528\u4e86\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}